package com.ruoyi.community.service.impl;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.*;
import com.ruoyi.community.mapper.OwnerActivityMapper;
import com.ruoyi.community.mapper.OwnerGroupMemberMapper;
import com.ruoyi.community.mapper.OwnerNoticeMapper;
import com.ruoyi.community.mapper.OwnerVoteMapper;
import com.ruoyi.community.service.IBaseNoticeService;
import com.ruoyi.community.service.IOwnerInfoService;
import com.ruoyi.community.service.IOwnerNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

//import java.security.acl.Owner;
import java.util.ArrayList;
import java.util.List;

@Service
public class OwnerInfoServiceImpl implements IOwnerInfoService {

    @Autowired
    private OwnerNoticeMapper ownerNoticeMapper;

    @Autowired
    private OwnerVoteMapper ownerVoteMapper;

    @Autowired
    private OwnerActivityMapper ownerActivityMapper;

    @Autowired
    private OwnerGroupMemberMapper ownerGroupMemberMapper;

    @Autowired
    private IBaseNoticeService baseNoticeService;


    @Override
    public List<OwnerInfoCardForApp> selectOwnerCardList() {
        List<OwnerInfoCardForApp> resultsList = new ArrayList<>();

        LoginUser loginUser = SecurityUtils.getLoginUser();
        String userId = loginUser.getUserId().toString();
        Long communityId = Long.valueOf(loginUser.getCommunityId());

//        OwnerNotice ownerNotice = new OwnerNotice();
//        ownerNotice.setCommunityId(communityId);
//        List<OwnerNotice> noticeList = ownerNoticeMapper.selectOwnerNoticeList(ownerNotice);
//        for (OwnerNotice item: noticeList){
//            OwnerInfoCardForApp ownerInfoCardForApp = new OwnerInfoCardForApp();
//            ownerInfoCardForApp.setId(item.getNoticeId());
//            ownerInfoCardForApp.setTitle(item.getTitle());
//            ownerInfoCardForApp.setType(0);
//            ownerInfoCardForApp.setTime(item.getSyncTime());
//            String content = item.getContent();
//            if(content.length() <= 40){
//                ownerInfoCardForApp.setPreview(content);
//            }else{
//                ownerInfoCardForApp.setPreview(content.substring(0, 40));
//            }
//            resultsList.add(ownerInfoCardForApp);
//        }
        BaseNotice baseNotice = new BaseNotice();
        baseNotice.setSectionId("2");
        baseNotice.setCommunityId(Math.toIntExact(communityId));
        List<BaseNotice> noticeList = baseNoticeService.selectBaseNoticePreviewWithoutPic(baseNotice);
        for (BaseNotice item: noticeList){
            OwnerInfoCardForApp ownerInfoCardForApp = new OwnerInfoCardForApp();
            ownerInfoCardForApp.setId(item.getNoticeId());
            ownerInfoCardForApp.setTitle(item.getTitle());
            ownerInfoCardForApp.setType(0);
            ownerInfoCardForApp.setTime(item.getSyncTime());
            String content = item.getContent();
//            if(content.length() <= 40){
//                ownerInfoCardForApp.setPreview(content);
//            }else{
//                ownerInfoCardForApp.setPreview(content.substring(0, 40));
//            }
            resultsList.add(ownerInfoCardForApp);
        }

        OwnerActivity ownerActivity = new OwnerActivity();
        ownerActivity.setCommunityId(communityId);
        List<OwnerActivity> activityList = ownerActivityMapper.selectOwnerActivityList(ownerActivity);
        for (OwnerActivity item: activityList){
            OwnerInfoCardForApp ownerInfoCardForApp = new OwnerInfoCardForApp();
            ownerInfoCardForApp.setId(item.getActivityId());
            ownerInfoCardForApp.setTitle(item.getTitle());
            ownerInfoCardForApp.setType(2);
            ownerInfoCardForApp.setTime(item.getSyncTime());
            String content = item.getContent();
//            if(content.length() <= 40){
//                ownerInfoCardForApp.setPreview(content);
//            }else{
//                ownerInfoCardForApp.setPreview(content.substring(0, 40));
//            }
            resultsList.add(ownerInfoCardForApp);
        }

        OwnerVote ownerVote = new OwnerVote();
        ownerVote.setCommunityId(communityId);
        List<OwnerVote> voteList = ownerVoteMapper.selectOwnerVoteList(ownerVote);
        for (OwnerVote item: voteList){
            String[] groups = item.getGroups().split(",");
            for(String groupId: groups){
                if(ownerGroupMemberMapper.isInGroup(userId, groupId)==1){
                    OwnerInfoCardForApp ownerInfoCardForApp = new OwnerInfoCardForApp();
                    ownerInfoCardForApp.setId(item.getVoteId());
                    ownerInfoCardForApp.setTitle(item.getTitle());
                    ownerInfoCardForApp.setType(1);
                    ownerInfoCardForApp.setTime(item.getSyncTime());
                    ownerInfoCardForApp.setPreview(item.getQuestion());
                    resultsList.add(ownerInfoCardForApp);
                    break;
                }
            }
        }

        return resultsList;
    }

    @Override
    public OwnerNotice selectOwnerNoticeByID(String id) {
        return ownerNoticeMapper.selectOwnerNoticeById(id);
    }

    @Override
    public OwnerVote selectOwnerVoteByID(String id) {
        return ownerVoteMapper.selectOwnerVoteById(id);
    }

    @Override
    public OwnerActivity selectOwnerActivityByID(String id) {
        return ownerActivityMapper.selectOwnerActivityById(id);
    }
}
